#include "include.h"

#define FS_CRC_SEED         0xffff

uint calc_crc(void *buf, uint len, uint seed);
bool mp3_res_play_kick(u32 addr, u32 len, bool kick);
bool esbc_res_play_kick(u32 addr, u32 len, bool kick);
void wav_res_play_kick(u32 addr, u32 len);
void wav_res_dec_process(void);
bool wav_res_is_play(void);
bool wav_res_stop(void);
void mp3_res_play_exit(void);
void esbc_res_play_exit(void);

#if FUNC_MUSIC_EN

//扫描全盘文件
bool pf_scan_music(u8 new_dev)
{
    if (new_dev) {
    }

    f_msc.file_total = fs_get_total_files();
    if (!f_msc.file_total) {
        f_msc.dir_total = 0;
        return false;
    }

    if (new_dev) {
#if MUSIC_PLAYDEV_BOX_EN
        gui_box_show_playdev();
#endif // MUSIC_PLAYDEV_BOX_EN

#if WARNING_USB_SD
        if (sys_cb.cur_dev <= DEV_SDCARD1) {
            mp3_res_play(RES_BUF_SDCARD_MODE_MP3, RES_LEN_SDCARD_MODE_MP3);
        } else {
            mp3_res_play(RES_BUF_USB_MODE_MP3, RES_LEN_USB_MODE_MP3);
        }
#endif // WARNING_USB_SD
    }

    f_msc.dir_total = fs_get_dirs_count();          //获取文件夹总数
    return true;
}

#if MUSIC_PLAYMODE_NUM
//music播放模式切换
void music_playmode_next(void)
{
    sys_cb.play_mode++;
    if (sys_cb.play_mode >= MUSIC_PLAYMODE_NUM) {
        sys_cb.play_mode = NORMAL_MODE;
    }
    gui_box_show_playmode();
    plugin_playmode_warning();
}
#endif // MUSIC_PLAYMODE_NUM

#if MUSIC_BREAKPOINT_EN
void bsp_music_breakpoint_clr(void)
{
    f_msc.brkpt.file_ptr = 0;
    f_msc.brkpt.frame_count = 0;
    param_msc_breakpoint_write();
    param_sync();
    //printf("%s\n", __func__);
}

void bsp_music_breakpoint_init(void)
{
    int clr_flag = 0;
    if (f_msc.brkpt_flag) {
        f_msc.brkpt_flag = 0;
        param_msc_breakpoint_read();
        //printf("restore: %d, %d, %04x\n", f_msc.brkpt.file_ptr, f_msc.brkpt.frame_count, f_msc.brkpt.fname_crc);
        if (calc_crc(f_msc.fname, 8, FS_CRC_SEED) == f_msc.brkpt.fname_crc) {
            music_set_jump(&f_msc.brkpt);
        } else {
            clr_flag = 1;
        }
    }
    f_msc.brkpt.fname_crc = calc_crc(f_msc.fname, 8, FS_CRC_SEED);
    f_msc.brkpt.file_ptr = 0;
    f_msc.brkpt.frame_count = 0;
    if (clr_flag) {
        param_msc_breakpoint_write();
    }
}

void bsp_music_breakpoint_save(void)
{
    music_get_breakpiont(&f_msc.brkpt);
    param_msc_breakpoint_write();
    param_sync();
    //printf("save: %d, %d, %04x\n", f_msc.brkpt.file_ptr, f_msc.brkpt.frame_count, f_msc.brkpt.fname_crc);
}
#endif // MUSIC_BREAKPOINT_EN
#endif // FUNC_MUSIC_EN

//播放RES MUSIC
bool bsp_res_music_play(u32 addr, u32 len, res_play_kick_func res_play_kick)
{
    if (len == 0 || !res_play_kick) {
        return true;
    }
    music_control(MUSIC_MSG_STOP);
    return res_play_kick(addr, len, 1);
}
 volatile u8 warning_ctl; //播放提示音的时候暂停 蓝牙数据的sbc解码

void res_music_playing_state_set(uint8_t mute)
{
    warning_ctl = mute;
}

u8 res_music_playing_state_get(void)
{
    return warning_ctl;
}
//打断音乐且阻塞式提示音播放，例如MP3
void msc_res_play_do(u32 addr, u32 len, res_play_kick_func res_play_kick, res_play_exit_func res_play_exit)
{
#if WARNING_TONE_EN
    u16 msg;
   // printf("%s: addr: %x, len: %x\n", __func__, addr, len);
    if (len == 0) {
        return;
    }

    //关闭sbc kick 上游链路
    res_music_playing_state_set(1);//warning_ctl = 1;

    dac_out_audio_mute_set(1);
    //避免硬件模块处于刚启动解码的临界值
    delay_ms(10);
    set_sys_clk(SYS_120M);
    //关闭 解码器
    if(get_music_dec_sta() != MUSIC_STOP) { //避免来电响铃/报号未完成，影响get_music_dec_sta()状态
        music_control(MUSIC_MSG_STOP);
    }
    //设置 提示音的音量大小
    bsp_change_volume(WARNING_VOLUME);
    // 设置提示音解码相关以及准备
    if (res_play_kick) {
        res_play_kick(addr, len, true);
    }
//    //解码 播放提示音过程中的消息处理在这里处理
    while (get_music_dec_sta() != MUSIC_STOP) {
        WDT_CLR();
        msg = msg_dequeue();
        if (msg != NO_MSG) {
            msg_enqueue(msg);       //还原未处理的消息
        }
    }

    if (res_play_exit) {
        res_play_exit();
    }
    void sbc_dec_reset(void);
    sbc_dec_reset();//复位一下sbc解码器配置

    dac_out_init(WIRELESS_MIC_SAMPLE_RATE_SELECT,WIRELESS_MIC_SAMPLES_SELECT);

    bsp_change_volume(sys_cb.vol);

    res_music_playing_state_set(0);//warning_ctl = 0;

    dac_out_audio_mute_set(0);
#endif // WARNING_TONE_EN
}

void mp3_res_play(u32 addr, u32 len)
{
    if (!wireless_role_is_adapter() && !DEVICE_DAC_OUTPUT_EN) {
        return;
    }
    msc_res_play_do(addr, len, mp3_res_play_kick, mp3_res_play_exit);
}

#if WARNING_ESBC_PLAY
void esbc_res_play(u32 addr, u32 len)
{
    if (!wireless_role_is_adapter() && !DEVICE_DAC_OUTPUT_EN) {
        return;
    }
    msc_res_play_do(addr, len, esbc_res_play_kick, esbc_res_play_exit);
}
#endif

void wav_res_play(u32 addr, u32 len)
{
    if (len == 0) {
        return;
    }

    if (!wireless_role_is_adapter() && !DEVICE_DAC_OUTPUT_EN) {
        return;
    }

    u8 clk_set = SYS_120M;
    sys_cb.wav_sysclk_bak = get_cur_sysclk();
    if (sys_cb.wav_sysclk_bak < clk_set) {
        set_sys_clk(clk_set);
    }

    wav_res_play_kick(addr, len);
    while (wav_res_is_play()) {
        wav_res_dec_process();
        WDT_CLR();
    }

    set_sys_clk(sys_cb.wav_sysclk_bak);
    sys_cb.wav_sysclk_bak = 0xff;

}
